<template>
	<view>
		<!-- 购物车按钮 -->
		<view class="shoppingCart">
			<text v-if="cartId==1" class="cart iconfont icon-gouwuche1" :style="[{color: color}]" @click.stop="onChange"></text>
			<text v-else-if="cartId==2" class="add iconfont icon-tianjia" :style="[{color: color}]" @click.stop="onChange"></text>
			<view v-else-if="cartId==3" class="btn" :style="[{
				border: '2rpx solid '+color,
				color: color,
			}]" @click.stop="onChange">立即购买</view>
			<view v-else class="btn" :class="{'integral-btn':cartBool==false}" :style="[{
				border: '2rpx solid '+color,
				color: color,
			}]" @click.stop="exchange">{{Btntitle}}</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	export default {
		name: 'shoppingCart',
		props:{
			//调用哪个按钮模块
			cartId:{
				type: Number,
				default: 1
			}, 
			//购买的产品信息
			Item: {
				type: Object,
				default: ()=>{}
			},
			//调用哪个按钮 true => 购买 false=> 兑换
			cartBool: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				border: '',
			};
		},
		// watch:{
		// 	color(){
		// 		let a = this.$store.state.color
		// 		console.log(this.color,a);
		// 	},
		// },
		computed:{
			...mapState(['color','Btntitle']),
		},
		methods:{
			//购买
			onChange(){
				let id = this.cartId==3? 0 : 1;
				this.buy(id,this.Item)
				console.log('------',id)
				uni.$emit('evenChange');//挂载在全局下
			},
			//积分兑换
			exchange(){
				uni.navigateTo({
					url: '/pages/Integral-mall/Integral-mall'
				})
			},
			//itemID => 0/1
			buy(itemID, item) {
				console.log(item);
				let id = uni.getStorageSync('LoginID');
				let token = uni.getStorageSync('Token');//Token值
				let hostid = uni.getStorageSync('hostid');//当前服务器id
				let oldHostid = uni.getStorageSync('Oldhostid');//上一次服务器id
				let corpid = uni.getStorageSync('corpid');//当前公司id
				let data = {};
				uni.request({
					url: this.$Url+'sale/CheckTokenLogin',
					data: '',
					method: 'POST',
					header:{
						'token': token,
						'cookie': uni.getStorageSync('Cookies'),
					},
					success: (res) => {
						if(res.data.msg=='success'&&id == 1){
							if (itemID == 0) {
								uni.navigateTo({
									url: `/pages/order/createOrder?id=1&&data=${JSON.stringify({goodsData: item})}`
								})
							} else {
								//corpid:公司id,goodsid:商品id,qty:数量,price:金额
									data = {
										corpid: corpid,
										goodsid: item.id,
										qty: 1,
										price: item.price,
									}
								console.log(item, data);
								this.$util.util('order/AddCart', data, 'POST').then(res => {
									console.log(res);
									this.$api.msg('加入购物车成功')
								}).catch(error=>console.log(error));
							}
						}else{
							uni.showModal({
								title: '提示',
								content: '尚未登录，是否先前去登录',
								success: (res) => {
									if (res.confirm) {
										uni.navigateTo({
											url: `/pages/public/login`
										})
									}
								}
							})
						}
					},
					fail: (error)=>console.log(error)
				})
			},
		}
	}
</script>

<style lang="scss">
	.shoppingCart{
		.cart,.add{
			width: 100%;
			height: 100%;
			font-size: 30px;
		}
		.btn{
			width: 128upx;
			height: 44upx;
			background: rgba(255,255,255,1);
			border: 2upx solid rgba(255,114,20,1);
			border-radius: 22upx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 12px;
			white-space: nowrap;
		}
		.integral-btn{
			width: auto;
			height: 30upx;
			padding: 0 5upx;
		}
	}
</style>
